
const cloud = require('wx-server-sdk')
var rp = require('request-promise');
// 初始化 cloud
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const getToken = function (APPID, APPSECRET){
  let url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${APPSECRET}`
  return rp(url).then(res=>{
    return res
  }).catch(err=>{
    console.log(err)
  })
};
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  console.log(event,context)
  let appid = wxContext.APPID
  let openid = wxContext.OPENID
  let unionid = wxContext.UNIONID
  let env = wxContext.ENV
  let appsecret = '9cc11657e7b17c1b86c3a454cbd63b6f';
  let data = await getToken(appid, appsecret);
  return {
    openid, data, unionid
  }

}

